//
//  WatchQRCodeViewController.m
//  leo
//
//  Created by RyanLin on 11/8/15.
//  Copyright © 2015 mdx. All rights reserved.
//

#import "WatchQRCodeViewController.h"

#import "AppDelegate.h"
#import "SyncManager.h"
#import "DataManager.h"
#import "LeoMsg.h"
#import "Tooler.h"
@interface WatchQRCodeViewController ()

@property (nonatomic,retain) SyncManager *syncManager;
@property (nonatomic,retain) DataManager *dataManager;

@end

@implementation WatchQRCodeViewController

@synthesize navLeftButton;


- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.title=@"二维码";
    [self initNavButton];
    
    //[unbindButton addTarget:self action:@selector(unbindAction) forControlEvents:UIControlEventAllTouchEvents];
    //获取二维码
    DataManager *dataManager=[[DataManager alloc] init];
    if(dataManager==NULL)
    {
        NSLog(@"%@,dataManager is null.",WatchQrcode_tag);
        [LeoMsg showMsg:@"获取二维码错误"];
        return;
    }
    User *user=[User sharedSingleton];
    if(user==NULL)
    {
        NSLog(@"%@,user is null.",WatchQrcode_tag);
        [LeoMsg showMsg:@"获取二维码错误"];
        return;
    }
    Watch *watch=[dataManager getActiveWatch:user.uid];
    if(watch==NULL)
    {
        NSLog(@"%@,watch is null.",WatchQrcode_tag);
        [LeoMsg showMsg:@"获取二维码错误"];
        return;
    }
    NSString *qrcoeUrl=watch.qrcode_url;
    if(qrcoeUrl!=NULL&&![qrcoeUrl isEqualToString:@"(null)"]&&![qrcoeUrl isEqualToString:@""])
    {
        self.qrcodeImg.image=[UIImage imageNamed:qrcoeUrl];
        return;
    }
    NSString *imei=watch.imei;
    if(watch.deviceUid==NULL||imei==NULL||[imei isEqualToString:@"(null)"]||[imei isEqualToString:@""])
    {
        NSLog(@"%@,imei is null.",WatchQrcode_tag);
        return;
    }
    self.qrcodeImg.image=[Tooler getQrcodeUrl:watch.deviceUid imei:imei size:self.qrcodeImg.bounds.size.width];
    // Do any additional setup after loading the view from its nib.
}
- (void)initNavButton {
    [navLeftButton setTarget:self];
    [navLeftButton setAction:@selector(back)];
}
-(void)unbindAction//没有用了
{
    User *user=[User sharedSingleton];
    NSString *userId=user.uid;
    Watch *watch=[self.dataManager getActiveWatch:userId];
    [self.syncManager unbindDevice:watch completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if (result==SUCESS)
        {
            [LeoMsg showMsg:@"解绑成功"];
        }
        else
        {
            [LeoMsg showMsg:@"解绑失败"];
            NSLog(@"%@",description);
        }
    }];//解除绑定 ok
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
    //[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
